React = require('react')
{connect} = require('react-redux')
{View,Text,Image,TextInput,TouchableOpacity} = require('react-native')
# Flex = require('antd-mobile/lib/flex')
Button = require('antd-mobile/lib/button')
Dimensions = require('Dimensions')
{ width, height } = Dimensions.get('window')

Toast = require('antd-mobile/lib/toast')
List = require('antd-mobile/lib/list')
InputItem = require('antd-mobile/lib/input-item')
Card = require('antd-mobile/lib/card')

{fetchChangePass} = require('../actions/userInfo.js')
actions = require('../actions/userInfo.js')
{getMenuFail} = require('../actions/menuInfo.js')
backImg = require('../img/back.png')

fetchChangePassword = (name,oldPwd,newPwd,token,successFunc)->
  str = "http://202.136.214.173/api/ChangePass?name=#{name}&oldPwd=#{oldPwd}&newPwd=#{newPwd}"
  fetch str,
    {
      method:'POST'
      mode:'cors'
      cache:'default'
      headers: {
        token:token
      }
    }
  .then (res)->
    debugger
    if res.ok
      res.json()
    else if res.status is 500
        Promise.reject new Error('用户名或密码错误')
    else
        Promise.reject new Error('网络请求失败,请重试')
  ,(err)->
    Promise.reject new Error('好像断网了哦')
  .then (json)->
    debugger
    if json.code is '1'
      successFunc()
    else
      if json.code is '2'
        Promise.reject new Error('密码强度不符合要求')
      else
        Promise.reject new Error('密码修改失败！')
  .catch (err)->
      Toast.fail err.message,->

Page_changePass = React.createClass
  getInitialState : ->
    oldPass:null
    newPass:null
    clicked:false
  clickHandle:->
    that = @
    unless @state.oldPass
      Toast.fail '请输入原密码'
      return
    unless @state.newPass
      Toast.fail '请输入新密码'
      return
    unless @state.newPass2
      Toast.fail '请再次输入新密码'
      return
    if @state.newPass2 isnt @state.newPass
      Toast.fail '两次密码输入的不一致！'
      return
    if @state.oldPass isnt @props.userInfo.pass
      Toast.fail '原密码错误！'
      return
    # @setState clicked:true
    dispatch = @props.dispatch
    dispatch actions.changePassStart()
    name = @props.userInfo.pilotCode
    pass = @state.newPass
    fetchChangePassword @props.userInfo.pilotCode,
      @props.userInfo.pass,
      @state.newPass,
      @props.userInfo.token,
      ->
        dispatch actions.changePassCompleted
          name:name
          pass:pass
        dispatch getMenuFail()
        Toast.success('密码修改成功，请重新登陆',3)
        setTimeout ->
          that.props.navigator.popToTop()
        ,1000
    # @props.dispatch fetchChangePass
    #   name:@props.userInfo.pilotCode
    #   oldPwd:@props.userInfo.pass
    #   newPwd:@state.newPass
    #   token:@props.userInfo.token
  componentDidUpdate:->
    # that = @
    # if @state.clicked and  @props.userInfo.pilotCode and @props.userInfo.pilotCode isnt 'guest'
    #   Toast.success('密码修改成功，请重新登陆',3)
    #   setTimeout ->
    #     # that.props.navigator.pop()
    #     that.props.navigator.popToTop()
    #   ,1000
  render:->
    # flexDirection:'column',alignItems:'center'
      <View style={{height:height,width:width,backgroundColor:'#ffffff'}}>
        <View style={{flexDirection:'row',flex:0,height:60,alignItems:'center', justifyContent:'center',backgroundColor:'#0066FF'}}>
          <View style={{flex:1,padding:10}}>
            <TouchableOpacity onPress={=>@props.navigator.pop()}>
              <Image source={backImg}  style={{height:30,width:30,resizeMode:'contain'}}/>
            </TouchableOpacity>
          </View>
          <View style={{flex:1,justifyContent:'center',alignItems:'center'}}>
            <Text style={{fontSize:20,color:'#ffffff'}}>修改密码</Text>
          </View>
          <View style={{flex:1,alignItems:'flex-end',padding:10}}>
          </View>
        </View>
        <Card>
          <Card.Header
            title="接上级部门通知，密码涉及用户账户的安全，需加强管理"
          />
          <Card.Body>
            <Text>   1、密码长度6~16位；</Text>
            <Text>   2、由大写字母、小写字母、数字、特殊字符至少两种组成；</Text>
            <Text style={{color:'red'}}>   注意：请事先在纸上记下新密码，然后再输入。</Text>
          </Card.Body>
        </Card>
        <List>
          <InputItem placeholder="请输入" autoCorrect={false} onChange={(text)=>@setState oldPass:text  } value={@state.oldPass}>原密码
          </InputItem>
          <InputItem placeholder="请输入" onChange={(text)=>@setState newPass:text } value={@state.newPass} type="password">新密码
          </InputItem>
          <InputItem placeholder="请输入" onChange={(text)=>@setState newPass2:text } value={@state.newPass2} type="password">新密码
          </InputItem>
          <View style={{margin:12}}>
            <Button type='primary' onClick={@clickHandle}>确定</Button>
          </View>
        </List>
      </View>

# <View style={{marginTop:10}}>
#   <TextInput secureTextEntry={true} style={{fontSize:16}} placeholder='请输入原密码' onChangeText={(text)=>@setState oldPass:text }></TextInput>
# </View>
# <View>
#   <TextInput secureTextEntry={true} style={{fontSize:16}} password=true placeholder='请输入新密码' onChangeText={(text)=>@setState newPass:text }></TextInput>
# </View>
# <View>
#   <TextInput secureTextEntry={true} style={{fontSize:16}} password=true placeholder='请再次输入新密码' onChangeText={(text)=>@setState newPass2:text }></TextInput>
# </View>
mapStateToProps = (state)->
  userInfo:state.userInfo
module.exports = connect(mapStateToProps)(Page_changePass)
